import Vue from 'vue'
import VueRouter from 'vue-router'
import Push from '../views/push.vue'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Push',
    component: Push
  },
  {
    path: '/found',
    name: 'Found',
    component: () => import('../views/found.vue')
  },
  {
    path: '/add',
    name: 'Add',
    component: () => import('../views/add.vue')
  },
  {
    path: '/message',
    name: 'Message',
    component: () => import('../views/message.vue')
  },
  {
    path: '/my',
    name: 'My',
    component: () => import('../views/my.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: ()=>import('../views/login.vue')
  },
  {
    path: '/myhome',
    name: 'myHome',
    component: ()=>import('../views/my/myHome.vue')
  },
  {
    path: '/edit',
    name: 'edit',
    component: ()=>import('../views/my/edit.vue')
  },
  {
    path: '/set',
    name: 'set',
    component: ()=>import('../views/my/set.vue')
  },
  {
    path: '/post',
    name: 'post',
    component: ()=>import('../views/my/post.vue')
  },
  {
    path: '/postComment',
    name: 'postComment',
    component: ()=>import('../views/my/postComment.vue')
  },
  {
    path: '/newsDetail',
    name: 'NewsDetail',
    component: () => import('@/views/push/newsDetail.vue')
  },
  {
    path: '/comments/:id',
    name: 'comments',
    component: () => import('@/views/push/comments.vue')
  },
  {
    path: '/found-detail/:id',
    name: 'found-detail',
    component: ()=>import('@/views/found/found-detail.vue')
  },
  {
    path: '/fans',
    name: 'fans',
    component: ()=>import('@/views/msg/fans.vue')
  },
  {
    path: '/chat/:id',
    name: 'chat',
    component: ()=>import('@/views/msg/chat.vue')
  }
  

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  // 1. 判断是不是登录页面
  // 是登录页面
  if(to.path === '/login') {
    next()
  } else {
    // 不是登录页面
    // 2. 判断 是否登录过
    let token = localStorage.getItem('token')
    if(token){
       next() 
    }else{
      alert('请先登录')
      next('/login')
    }
  }
})
export default router
